package com.ruoyi.map.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.map.domain.BuildingInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.map.mapper.ApplyInvestigationMapper;
import com.ruoyi.map.domain.ApplyInvestigation;
import com.ruoyi.map.service.IApplyInvestigationService;

/**
 * 申请调查Service业务层处理
 *
 * @author dihang
 * @date 2023-08-16
 */
@Service
public class ApplyInvestigationServiceImpl extends ServiceImpl<ApplyInvestigationMapper, ApplyInvestigation> implements IApplyInvestigationService
{
    @Autowired
    private ApplyInvestigationMapper applyInvestigationMapper;

    /**
     * 查询个人申请各个家庭年人均收入数量（饼状图）
     * @return
     */
    @Override
    public List<Map<String, Object>> countByAAnnualIncome() {
        List<ApplyInvestigation> list = applyInvestigationMapper.selectList(null);
        Map<String ,Integer>countMap=new HashMap<>();
        for (ApplyInvestigation applyInvestigation : list) {
            String annualIncome = applyInvestigation.getAnnualIncome();
            countMap.put(annualIncome,countMap.getOrDefault(annualIncome,0)+1);

        }
        List<Map<String ,Object>>resultMap=new ArrayList<>();
        for (String annualIncome : countMap.keySet()) {
            Map<String ,Object>item=new HashMap<>();
            item.put("annualIncome",annualIncome);
            item.put("count",countMap.get(annualIncome));
            resultMap.add(item);
        }
        if (!countMap.containsKey("0")){
            Map<String, Object> aa = new HashMap<>();
            aa.put("annualIncome",'0');
            aa.put("count",0);
            resultMap.add(aa);
        }
        if (!countMap.containsKey("1")){
            Map<String, Object> bb = new HashMap<>();
            bb.put("annualIncome",'1');
            bb.put("count",0);
            resultMap.add(bb);
        }
        if (!countMap.containsKey("2")){
            Map<String, Object> cc = new HashMap<>();
            cc.put("annualIncome",'2');
            cc.put("count",0);
            resultMap.add(cc);
        }
        if (!countMap.containsKey("3")){
            Map<String, Object> dd = new HashMap<>();
            dd.put("annualIncome",'3');
            dd.put("count",0);
            resultMap.add(dd);
        }

        return resultMap;
    }

    /**
     * 查询个人申请各个家庭年人总收入数量（柱状图）
     * @return
     */
    @Override
    public List<Map<String, Object>> countByTotalIncome() {
        List<ApplyInvestigation> list = applyInvestigationMapper.selectList(null);
        Map<String ,Integer>countMap=new HashMap<>();
        for (ApplyInvestigation applyInvestigation : list) {
            String totalIncome = applyInvestigation.getTotalIncome();
            countMap.put(totalIncome,countMap.getOrDefault(totalIncome,0)+1);

        }
        List<Map<String ,Object>>resultMap=new ArrayList<>();
        for (String totalIncome : countMap.keySet()) {
            Map<String ,Object>item=new HashMap<>();
            item.put("totalIncome",totalIncome);
            item.put("count",countMap.get(totalIncome));
            resultMap.add(item);
        }
        if (!countMap.containsKey("0")){
            Map<String, Object> aa = new HashMap<>();
            aa.put("totalIncome",'0');
            aa.put("count",0);
            resultMap.add(aa);
        }
        if (!countMap.containsKey("1")){
            Map<String, Object> bb = new HashMap<>();
            bb.put("totalIncome",'1');
            bb.put("count",0);
            resultMap.add(bb);
        }
        if (!countMap.containsKey("2")){
            Map<String, Object> cc = new HashMap<>();
            cc.put("totalIncome",'2');
            cc.put("count",0);
            resultMap.add(cc);
        }
        if (!countMap.containsKey("3")){
            Map<String, Object> dd = new HashMap<>();
            dd.put("totalIncome",'3');
            dd.put("count",0);
            resultMap.add(dd);
        }

        return resultMap;

    }

    /**
     * 查询申请调查列表
     *
     *
     * @param applyInvestigation 申请调查
     * @return 申请调查
     */
    @Override
    public IPage<ApplyInvestigation> selectApplyInvestigationList(ApplyInvestigation applyInvestigation)
    {

        IPage<ApplyInvestigation>page=this.page(new Page<>(applyInvestigation.getPageNum(),
                applyInvestigation.getPageSize()),oneSelectWhere(applyInvestigation));
        return page;
    }

    private LambdaQueryWrapper<ApplyInvestigation> oneSelectWhere(ApplyInvestigation applyInvestigation) {
        LambdaQueryWrapper<ApplyInvestigation>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (applyInvestigation==null){
            return lambdaQueryWrapper;
        }
        //姓名模糊查询
        lambdaQueryWrapper.like(StrUtil.isNotBlank(applyInvestigation.getName()),
                ApplyInvestigation::getName,applyInvestigation.getName());

        //家庭人均年收入
        if (applyInvestigation.getAnnualIncome()!=null){
         lambdaQueryWrapper.eq(ApplyInvestigation::getAnnualIncome,applyInvestigation.getAnnualIncome());
        }
        //家庭年总收入
        if (applyInvestigation.getTotalIncome()!=null){
            lambdaQueryWrapper.eq(ApplyInvestigation::getTotalIncome,applyInvestigation.getTotalIncome());
        }
        //性别
        if (applyInvestigation.getGender()!=null){
            lambdaQueryWrapper.eq(ApplyInvestigation::getGender,applyInvestigation.getGender());
        }
        //家庭人均建筑面积
        if (applyInvestigation.getAnnualArea()!=null) {
            lambdaQueryWrapper.eq(ApplyInvestigation::getAnnualArea,applyInvestigation.getAnnualArea());
        }
        //未删除
               lambdaQueryWrapper .eq(ApplyInvestigation::getDeleted,"0");
        //时间倒叙
        lambdaQueryWrapper.orderByDesc(ApplyInvestigation::getCreateTime);
        return lambdaQueryWrapper;
    }

}
